; some file related poor procs
read_texture:
;  in - esi ptr to texture file
;       ecx - texture file size  in bytes
   push        ebp
   mov         ebp,esp
   .i26        equ dword [ebp-4]
   .odd        equ dword [ebp-8]
   .endf       equ dword [ebp-12]
   .file_ptr   equ dword [ebp-16]
   .file_size  equ dword [ebp-20]
   .ennnnnn11  equ dword [ebp-20]
   .height     equ dword [ebp-28]
   .width      equ dword [ebp-32]
   .lh         equ dword [ebp-36]   ; lower height
   .lw         equ dword [ebp-40]   ; lower width
   .bpp        equ dword [ebp-44]
   .pal        equ dword [ebp-48]
   .begin      equ dword [ebp-52]
   .rle        equ dword [ebp-56]
   .restx      equ dword [ebp-60]
   .resty      equ dword [ebp-64]
   push    dword 26
   push    dword 0   ; odd
   lea     ebx,[esi+ecx]
   push    ebx       ; end
   push    esi       ; f ptr
   push    ecx       ; f size
   push    ecx       ; free   space
   cld
   cmp     word[esi],0x4d42  ;4d;42
   jne    .raw
 .bmp:
   mov     edi,20   ; os2
   mov     ecx,22   ; win
   cmp     byte[esi+24],0 ; is os2 version ?
   cmove   edi,ecx
   movzx   eax,word[esi+edi]
   movzx   ebx,word[esi+18]
   push    eax              ; h
   push    ebx              ; w
   mov     ecx,TEX_X
   mov     edx,TEX_Y
   cmp     eax,edx          ; TEX_Y
   cmova   eax,edx
   push    eax              ; lower h
   cmp     ebx,ecx          ; TEX_X
   cmova   ebx,ecx
   push    ebx              ; lower w
   mov     ebx,28
   mov     ecx,24
   cmp     edi,20
   cmove   ebx,ecx
   movzx   eax,byte[esi+ebx]
   push    eax  ; bpp
   cmp     eax,1
   je      .1_bit_bmp
 ;  cmp     eax,18
 ;  je      .24_bit_bmp
   ;4/8 bit bmp
   mov     ebx,794
   mov     ecx,1078
   mov     edx,54
   cmp     edi,20
   cmove   ecx,ebx   ; ecx - begin of data
   cmove   edx,.i26  ; edx - palette begin
   push    edx       ; pal
   push    ecx       ; dat begin
   mov     eax,22
   mov     ebx,30
   cmp     edx,26
   cmove   ebx,eax      ; win/os2 cause - compression
   movzx   ebx,byte[esi+ebx]
   push    ebx
   sub    esp,12
   mov    eax,TEX_X   ;.lw
   sub    eax,.width
   shl    eax,2
   mov    .restx,eax  ; if negative - .width < .lh
  @@:
   mov    eax,TEX_Y   ;.lh
   sub    eax,.height
   shl    eax,2
   mov    .resty,eax  ; if negative - .height < .lw
   mov    ebx,.width
   imul   ebx,.height
   mov    eax,.bpp
   cmp    eax,4
   je     .4_bit_bmp
   cmp    eax,24
   je     .24_bit_bmp
   cmp    eax,32
   je     .32_bit_bmp
   cmp    eax,8
   je     @f
   jmp    .end
 @@:
 ;  cmp    .rle,1
 ;  je     @f
   cmp    ebx,.file_size   ; TEXTURE_SIZE
   jnb    .end             ; prevent rle compression
  @@:
   mov    edi,texmap       ;  8 bit cause
   mov    ebx,.pal
   add    ebx,esi
   add    esi,.begin
   mov    ecx,.height
   cmp    .pal,26
   jne    .non_os2_8_bmp
  ; 8 bit os2 ver
  .bmp_8_os2_l:
   push   ecx
   mov    ecx,.width
  @@:
   xor    eax,eax
   lodsb
   lea    eax,[eax*3]
   mov    eax,[ebx+eax]
   stosd
   loop   @b
   add    edi,.restx
   pop    ecx
   loop   .bmp_8_os2_l
   jmp    .end
 .non_os2_8_bmp:
   sub  .endf,2
 ; 8 bit win
   cld
 .bmp_8_win_l:
   push   ecx
   mov    ecx,.width
 .bmp8wl2:
   xor    eax,eax
   lodsb
   shl    eax,4
   mov    eax,[ebx+eax]
   stosd
  .nxl:
   loop   .bmp8wl2
   add    edi,.restx
   pop    ecx
   loop   .bmp_8_win_l
   jmp    .end
 .1_bit_bmp:
   jmp    .end
 .4_bit_bmp:
   shr    ebx,1
   cmp    ebx,.file_size  ;TEXTURE_SIZE
   jnb    .end      ; prevent rle compression
   mov    eax,.pal
   lea    ebx,[eax+16*3]
   lea    ecx,[eax+16*4]
   cmp    byte[esi+24],0
   cmove  ebx,ecx
   mov    .begin,ebx
   mov    edi,texmap
   mov    ebx,.pal
   add    ebx,esi
   mov    .odd,0
   mov    eax,.lw
   shr    eax,1
   adc    .odd,0
   mov    ecx,.height
   cmp    byte[esi+24],0
   jne    .4_bit_bmp_os2
 .non_os2_4_bmp:
   ; 4 bit win
   add    esi,.begin
 .bmp_4_win_l:
   push   ecx
   mov    ecx,.width
  @@:
   xor    eax,eax
   lodsb
   mov    edx,eax
   and    eax,0xf
   shr    edx,8
   shl    eax,2
   shl    edx,2
   mov    eax,[ebx+eax]
   stosd
   dec    ecx
   jecxz  @f
   mov    eax,[ebx+edx]
   stosd
   loop   @b
  @@:
   add    edi,.restx
   pop    ecx
   loop   .bmp_4_win_l
   jmp    .end
  .4_bit_bmp_os2:
   add    esi,.begin
;   mov    ecx,.height
 .bmp_4_os2_l:
   push   ecx
   mov    ecx,.width
  @@:
   xor    eax,eax
   lodsb
   mov    edx,eax
   and    eax,0xf
   shr    edx,8
   lea    eax,[eax*3]
   lea    edx,[edx*3]
   mov    eax,[ebx+eax]
   stosd
   dec    ecx
   jecxz  @f
   mov    eax,[ebx+edx]
   stosd
   loop   @b
  @@:
   add    edi,.restx
   pop    ecx
   loop   .bmp_4_os2_l
   jmp    .end
 .24_bit_bmp:
   cld
   lea     ebx,[ebx*3]
   cmp     ebx,.file_size  ;TEXTURE_SIZE
   jnb     .end      ; prevent  compression
   mov     edi,texmap
   add     edi,TEX_X*TEX_Y*4
   xor     eax,eax
   mov     [edi],eax
   add     esi,.pal
   mov     ecx,.height
  .bmp_nx24_bit:
   push    ecx
   mov     ecx,.width
  .bmp_nx24_bit2:
  @@:
   cld
   lodsd
   dec     esi
   std
   stosd
   loop    @b
   sub     edi,.restx
   pop     ecx
   loop    .bmp_nx24_bit
   stosd
   jmp     .end
 .32_bit_bmp:
   mov     edi,texmap
   add     edi,TEX_X*TEX_Y*4
   add     esi,.pal
   mov     ecx,.height
 .bmp_nx32_bit:
   push    ecx
   mov     ecx,.width
  @@:
   cld
   lodsd
   std
   stosd
   loop    @b
   sub     edi,.restx
   pop     ecx
   loop    .bmp_nx32_bit
   jmp     .end
 .raw:
   mov     esi,.file_ptr
   mov     edi,texmap
   cld
   cmp     .file_size,TEX_X*TEX_Y*3
   jnae    .end
   mov     ecx,TEX_X*TEX_Y
   xor     eax,eax
 @@:
   movsw
   movsb
   stosb
   loop    @b
 .end:
   cld
   mov     esp,ebp
   pop     ebp
ret
;===============================================================================
save_fileA:
; in:
;    edx - buffer to save
;    eax - file_name (aciiz)
;    ecx - data size in bytes
    push      ebp
    mov       ebp,esp
    sub       esp,4
   .file_handle equ dword[ebp-4]
    push      edx ecx
    invoke    CreateFile,eax,GENERIC_WRITE, 0, 0,CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0
    pop       ecx edx
    mov       .file_handle,eax
    invoke    WriteFile,.file_handle ,edx,ecx,byteswritten, 0
    invoke    CloseHandle,.file_handle
    mov       esp,ebp
    pop       ebp
ret
;=========================================================================
; one ugly thing - wrong verts begin calc in 'm' ply cause..
;==============================================================================
open_file:
   ; in:
   ;   xmm0 = file params pack, see below
   .ofn       equ dword[ebp-4]
   .fname     equ dword[ebp-8]
   .hfile     equ dword[ebp-12]
   .FileSize  equ dword[ebp-16]
   .File_ptr  equ [ebp-20]
   .fail_msg  equ [ebp-24]
   push     ebp
   mov      ebp,esp
   sub      esp,80
   movups   .File_ptr,xmm0
   mov      .ofn,ecx
   mov      ebx,.File_ptr
;   mfree    [ebx]
 ;  mov     .ofn,      ofn
 ;  mov     .fname,    fname
 ;  mov     .hfile,    hfile
 ;  mov     .FileSize, FileSize
 ;  mov     dword .File_ptr, File_ptr
   virtual   at   ecx
   ofn_ecx   OPENFILENAME
   end       virtual
   mov       ecx,.ofn
   mov       [ofn_ecx.Flags],OFN_FILEMUSTEXIST+OFN_PATHMUSTEXIST+OFN_EXPLORER
   mov       ecx,.ofn
   invoke    GetOpenFileName,.ofn
   or        eax,eax
   jz        .cancel
   invoke    CreateFile,.fname,GENERIC_READ,FILE_SHARE_READ,0,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0
   mov       ebx,.hfile
   mov       [ebx],eax
   cmp       eax,-1
   je        .failed
   invoke    GetFileSize,[ebx],0
   cmp       eax,-1
   je        .failed
   mov       edx,.FileSize
   mov       [edx],eax
   malloc    eax
   mov       edx,.File_ptr
   mov       [edx],eax
   mov       ecx,.FileSize
   invoke    ReadFile,[ebx],eax,[ecx],BytesRead,0
   or        eax,eax
   jz        .read_error
   mov       ebx,.hfile
   invoke    CloseHandle,[ebx]
 ;  xor      eax,eax
 ;  mov      dword[ebx],eax
   mov       eax,[BytesRead]
;   or       eax,eax
;   jz       .failed
;   mov      ebx,.File_ptr
;   mov      ebx,[ebx]
;   xor      esi,esi
;   mov      eax,1
;   mov      edx,2
;   mov      cx,WORD [ebx]
;   cmp      cx,'MM'
;   cmove    eax,esi
;   cmp      cx,'pl'
;   cmove    eax,edx
;   mov      [file_ext_flag],al
 .init_ptr:
   invoke    InvalidateRect,[hwnd],0,1
   xor       eax,eax
   jmp       .en
 .read_error:
 .failed:    ; always crash if not aprporiate file
             ; think about generating object ....
   mov       eax,.hfile
   or        eax,eax
   jz        @f
   invoke    CloseHandle,[eax]
  @@:
   invoke    MessageBox,[hwnd],_ferr,0,MB_OK
  .cancel:
  ; invoke   ReleaseDC,[hwnd],[hdc]
  ; invoke   DestroyWindow,[hwnd]
  ; invoke   ExitProcess,[msg.wParam]
 .en:
   add       esp,80
   pop       ebp
ret
;==========  ==============================










